/*
 * CircleDocument.java
 *
 * Created on November 3, 2006, 8:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler.circle;

import edu.umn.cs5115.scheduler.framework.Document;
import edu.umn.cs5115.scheduler.framework.KeyValueCoding;
import edu.umn.cs5115.scheduler.framework.KeyValueObserver;
import edu.umn.cs5115.scheduler.framework.KeyValueObserver.SetMutationType;

import java.util.Set;

/**
 * A subclass of Document that defines behavior for creating window controllers,
 * and nothing else.
 * @author grant
 */
public class CircleDocument extends Document
{
    private Circle theCircle = new Circle(this);
    
    // Only purpose of this is to not do the circle set test every time a new
    // document is created.
    static boolean firstTime = true;
    
    public CircleDocument()
    {
        if (firstTime)
        {
            firstTime = false;
            // This just tests some code dealing with the CircleSet.  It isn't
            // relevant for the CircleDocument that is actually used in the interface,
            // it just tests some of the set code.
            // Disable undo first so that the document doesn't start out modified.
            getUndoManager().disableUndoRegistration();
            CircleSet set = new CircleSet(this, theCircle);

            // add a listener to the set's CIRCLES_KEY.
            set.addListener(new KeyValueObserver()
            {
                // This method won't be called, since we're listening to a set.
                public void valueChanged(KeyValueCoding object, String key)
                {
                    System.out.println("This won't be printed.");
                }

                // This is where notifications will show up
                public void valueChanged(KeyValueCoding object, String key, SetMutationType mutation, Set modifyingSet)
                {
                    System.out.println("The set was just modified using the " + mutation + " operator and the objects " + modifyingSet);
                }
            }, CircleSet.CIRCLES_KEY);

            // try (unsuccessfully) to remove the initial circle.
            set.removeCircle(theCircle);

            // add some stuff to the set.
            Circle circleOne = new Circle(this);
            circleOne.setName("Circle 1");
            set.addCircle(circleOne);

            // remove the original circle - this time we succeed.
            set.removeCircle(theCircle);

            // try adding circleOne again.
            set.addCircle(circleOne);
            getUndoManager().enableUndoRegistration();
        }
    }

    protected void createWindowControllers()
    {
        CircleDocumentWindowController mainWindowController = new CircleDocumentWindowController(this);
        mainWindowController.showWindow();
        CirclePreviewWindowController previewWindowController = new CirclePreviewWindowController(this);
        previewWindowController.showWindow();
    }
    
    public Circle getCircle()
    {
        return theCircle;
    }
    
    /**
     * Allow pristine document behavior.  If the first thing you do is open a
     * document, the document created by default will be used to open it.
     * @return True.
     */
    public boolean isPristineBehaviorSupported()
    {
        return true;
    }
}
